﻿using Common.DTO;
using Database.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Database.Repositories
{
    public class RepositoryBase<T>
    {
        private const string connStr = @"Data Source=OSKARASUS;Initial Catalog=TestDb;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework;";

        private readonly Mapper _mapper;
        private Model1Container _model1Container;

        public RepositoryBase()
        {
            _model1Container = new Model1Container();
            _mapper = new Mapper();

            //var connStr = @"Data Source=OSKARASUS;Initial Catalog=TestDb;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework;";
            _model1Container.Database.Connection.ConnectionString = connStr;

        }

        public Model1Container DbContext { get { return _model1Container; } }
        public Mapper Mapper { get { return _mapper; } }

        public virtual bool MarkEntityForDeletion(T entity)
        {
            return false;
        }

        protected virtual void MarkEntityDependenciesForDeletion()
        {
            
        }

        protected bool SaveChanges(int expectedRowsAffected = 1)
        {
            var rowsAffected = _model1Container.SaveChanges();
            _model1Container.Database.Connection.Close();
            //_model1Container.Database.Connection.Dispose();
            if (rowsAffected == expectedRowsAffected)
                return true;
            else
                return false;
        }

        protected void RefreshDb(bool force = true)
        {
            _model1Container.Dispose();
            _model1Container = new Model1Container();
            _model1Container.Database.Connection.ConnectionString = connStr;
            //_model1Container.Database.Connection.Open();
        }
    }
}
